import CommonHelper from '../utils/common.helper'

export class PlatformBaseManager {
  protected viewContainer: JQuery<HTMLElement>
  init() {}
  constructor() {
    this.viewContainer = jQuery('<div class="ecommercy-container"></div>').css({
      position: 'fixed',
      'z-index': 10000,
    })
    this.viewContainer.prependTo(document.body)
  }
  async saveProductInfo(productInfo: ProductInfo) {
    let parentHandle = await window.showDirectoryPicker()
    if (!(await CommonHelper.verifyPermission(parentHandle))) {
      return
    }

    let dirHandle = await parentHandle.getDirectoryHandle(productInfo.title, {
      create: true,
    })

    CommonHelper.saveFile(
      'index.html',
      this.renderProductContent(productInfo),
      dirHandle
    )

    productInfo.imgList.map(({ fileName, imgUrl }) => {
      CommonHelper.saveFileFromUrl(fileName, imgUrl, dirHandle)
    })
    productInfo.tierList.map((tier) => {
      tier.options.map(({ fileName, imgUrl }) => {
        CommonHelper.saveFileFromUrl(fileName, imgUrl, dirHandle)
      })
    })
  }

  renderProductContent(productInfo: ProductInfo) {
    let pageContent = `
  <title>${productInfo.title}</title>
  <div style="max-width:1180px;margin:0 auto">
  <h2><a href="${productInfo.href}">${productInfo.title}</a></h2>
  <h3>商品图片</h3>
  <div>
  ${productInfo.imgList
    .map((img) => `<img style="width: 100px" src="${img.fileName}">`)
    .join('')}
  </div>
  ${productInfo.tierList
    .map((tier) => {
      let tierContent = tier.options
        .map(
          (option) => `
  <div style="display:inline-block;margin-right:10px">
     <p>${option.title}</p>
     <img style="width: 100px" src="${option.fileName}" download="${option.fileName}">
  </div>
  `
        )
        .join('')
      return `
  <h4>${tier.title}</h4>
  <div>${tierContent}</div>
  </div>
  `
    })
    .join('')}
  `
    return pageContent
  }

  saveOrderList(orderList: OrderInfo[]) {
    let bodyTemplate = orderList
      .map(
        (order) => `
    <tr>
        <td data-desc="订单编号">${order.orderSn}</td>
        <td data-desc="运单编号">${order.trackingNo}</td>
        <td data-desc="收件人">${order.buyerName}</td>
        <td data-desc="收货地邮编">${order.addressZipcode}</td>
        <td data-desc="收件人电话">${order.buyerPhone}</td>
        <td data-desc="收件省份">${order.addressState}</td>
        <td data-desc="收件城市">${order.addressCity}</td>
        <td data-desc="收件地区">${order.addressDistrice}</td>
        <td data-desc="收件详细地址">${order.buyerAddress}</td>
        <td data-desc="重量KG"></td>
        <td data-desc="体积"></td>
        <td data-desc="代收货款"></td>
        <td data-desc="货物类型">普货</td>
        <td data-desc="备注"></td>
        <td data-desc="中文品名">玩具</td>
        <td data-desc="英文品名">toys</td>
        <td data-desc="件数">${order.amount}</td>
        <td data-desc="申报价值">${order.totalPrice}</td>
        <td data-desc="申报价值币别">${order.currency}</td>
        <td data-desc="物品分类1"></td>
        <td data-desc="物品分类2"></td>
        <td data-desc="物品分类3"></td>
        <td data-desc="商品备注"></td>
        <td data-desc="购买类型"></td>
        <td data-desc="URL">${order.productUrl}</td>
        <td data-desc="出口商品类型"></td>
        <td data-desc="商品规格"></td>
    </tr>
            `
      )
      .join('')
    let resultContent = `<table>
    <thead>
          <tr>
          <th>订单编号</th>
          <th>运单编号</th>
          <th>收件人</th>
          <th>收货地邮编</th>
          <th>收件人电话</th>
          <th>收件省份</th>
          <th>收件城市</th>
          <th>收件地区</th>
          <th>收件详细地址</th>
          <th>重量KG</th>
          <th>体积</th>
          <th>代收货款</th>
          <th>货物类型</th>
          <th>备注</th>
          <th>中文品名</th>
          <th>英文品名</th>
          <th>件数</th>
          <th>申报价值</th>
          <th>申报价值币别</th>
          <th>物品分类1</th>
          <th>物品分类2</th>
          <th>物品分类3</th>
          <th>商品备注</th>
          <th>购买类型</th>
          <th>URL</th>
          <th>出口商品类型</th>
          <th>商品规格</th>
          </tr>
    </thead>
    <tbody>${bodyTemplate}</tbody>
</table>
`

    let dateString = new Date().toLocaleDateString().replace(/\//g, '_')
    CommonHelper.saveFile(
      `order-list_${dateString}.xls`,
      resultContent
      //   'application/vnd.ms-excel'
    )
  }
}
